package com.liangchen.astools.info

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.liangchen.astools.R
import com.liangchen.astools.info.AsInfoManager.InfoItem

class InfoItemAdapter(private val mItemList: List<InfoItem>) : RecyclerView.Adapter<InfoItemAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.layout_recyclerview_info_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = mItemList[position]
        holder.setData(item)
    }

    override fun getItemCount(): Int {
        return mItemList.size
    }

    class ViewHolder(var singleItemView: View) : RecyclerView.ViewHolder(singleItemView) {
        var textViewItemName: TextView
        var textViewItemValue: TextView
        var textViewItemCategory: TextView
        fun setData(item: InfoItem) {
            if (item.isTitle) {
                textViewItemCategory.visibility = View.VISIBLE
                textViewItemName.visibility = View.GONE
                textViewItemValue.visibility = View.GONE
                textViewItemCategory.text = item.itemName
            } else {
                textViewItemCategory.visibility = View.GONE
                textViewItemName.visibility = View.VISIBLE
                textViewItemName.text = item.itemName
                textViewItemValue.visibility = View.VISIBLE
                textViewItemValue.text = item.itemValue
            }
        }

        init {
            textViewItemCategory = itemView.findViewById(R.id.textview_category_name)
            textViewItemName = itemView.findViewById(R.id.textview_info_name)
            textViewItemValue = itemView.findViewById(R.id.textview_info_value)
        }
    }
}